package com.mall.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.Result;
import com.mall.dto.OrderDTO;
import com.mall.model.Order;
import com.mall.model.OrderItem;
import com.mall.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "订单管理")
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {
    
    private final OrderService orderService;
    
    @Operation(summary = "创建订单")
    @PostMapping
    public Result<Void> createOrder(@Valid @RequestBody OrderDTO orderDTO) {
        Order order = new Order();
        // 设置用户ID
        order.setUserId(StpUtil.getLoginIdAsLong());
        // 设置收货信息
        order.setAddress(orderDTO.getAddress());
        order.setReceiver(orderDTO.getReceiver());
        order.setPhone(orderDTO.getPhone());
        // 设置订单详情
        List<OrderItem> items = orderDTO.getItems().stream()
                .map(item -> {
                    OrderItem orderItem = new OrderItem();
                    orderItem.setProductId(item.getProductId());
                    orderItem.setQuantity(item.getQuantity());
                    return orderItem;
                })
                .collect(Collectors.toList());
//        order.setItems(items);
        // 创建订单
        orderService.createOrder(order);
        return Result.success();
    }
    
    @Operation(summary = "取消订单")
    @PostMapping("/{id}/cancel")
    public Result<Void> cancelOrder(@PathVariable Long id) {
        orderService.cancelOrder(id);
        return Result.success();
    }
    
    @Operation(summary = "支付订单")
    @PostMapping("/{id}/pay")
    public Result<Void> payOrder(@PathVariable Long id) {
        orderService.payOrder(id);
        return Result.success();
    }
    
    @Operation(summary = "发货")
    @PostMapping("/{id}/ship")
    public Result<Void> shipOrder(@PathVariable Long id) {
        orderService.shipOrder(id);
        return Result.success();
    }
    
    @Operation(summary = "确认收货")
    @PostMapping("/{id}/confirm")
    public Result<Void> confirmOrder(@PathVariable Long id) {
        orderService.confirmOrder(id);
        return Result.success();
    }
    
    @Operation(summary = "分页查询用户订单")
    @GetMapping("/page")
    public Result<Page<Order>> getUserOrderPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer status) {
        Page<Order> page = new Page<>(current, size);
        return Result.success(orderService.getUserOrderPage(page, StpUtil.getLoginIdAsLong(), status));
    }
} 